package com.mulanweb.api.module.controller;

import com.jfinal.plugin.activerecord.Page;
import com.mulanweb.api.module.model.Coupon;
import com.mulanweb.api.module.model.CouponJoinUser;
import com.mulanweb.api.module.model.Seller;
import com.mulanweb.api.module.model.Ticket;
import com.mulanweb.db.common.SQLGenerateHelper;
import com.mulanweb.validator.annotation.ValidateFile;
import com.mulanweb.validator.annotation.ValidateForm;

import java.util.Date;
import java.util.Random;

/**
 * Created by Administrator on 2016/8/19.
 */
@ValidateFile
public class CouponController extends BaseController {

    @ValidateForm
    public void save() {
        Float value = Float.parseFloat(getPara("value"));
        Integer extensionId = getParaToInt("extensionId");
        Integer totalNumber = getParaToInt("totalNumber");
        Integer surplusNumber = totalNumber;
        Integer typeId = getParaToInt("typeId");
        Float satisfyValue =Float.parseFloat(getPara("satisfyValue"));
        Date startTime = getParaToDate("startTime");
        Date endTime = getParaToDate("endTime");
        Long statusId = 10L;//默认状态为 有效
        String couponCode = createCouponCode();
        Coupon coupon = Coupon.dao.findFirst("select * from coupon where coupon_code = ?",couponCode);
        while(coupon !=null){
            //此优惠券码已存在，请重新生成
            couponCode = createCouponCode();
        }
        Long adminId = 1L ;//测试管理员主键
        coupon = new Coupon();
        coupon.setTypeId(typeId);
        coupon.setValue(value);
        coupon.setTotalNumber(totalNumber);
        coupon.setSurplusNumber(surplusNumber);
        coupon.setSatisfyValue(satisfyValue);
        coupon.setStatusId(statusId);
        coupon.setCouponCode(couponCode);
        coupon.setStartTime(startTime);
        coupon.setEndTime(endTime);
        coupon.setExtensionId(extensionId);
        coupon.setCreateAdmin(adminId);
        coupon.setCreateTime(new Date());
        if(coupon.save()){
            returnSuccessJson();
        }else{
            returnFailureJson();
        }
    }

    @ValidateForm
    public void search() {
        SQLGenerateHelper helper = new SQLGenerateHelper("coupon","create_time");
        helper.addEqualsCondition("coupon_code",getParaToLong("couponCode"));
        helper.addEqualsCondition("type_id",getParaToLong("typeId"));
        helper.addIsNullCondition("delete_time");
        Page<Coupon> page = Coupon.dao.paginate(getPageNumber(),getPageSize(),helper.getSqlSelect() , helper.getSqlExceptSelect(), helper.getQueryParas());
        returnPageModelJson(page);
    }

    private String createCouponCode(){
        Random rand = new Random();
        int max = 99999999;
        int min = 10000000;
        int tmp = Math.abs(rand.nextInt());
        Integer code = tmp % (max - min + 1) + min;
        return code.toString() ;
    }

    public void update(){
        Integer couponId = getParaToInt("couponId");
        Date startTime = getParaToDate("startTime");
        Date endTime = getParaToDate("endTime");
        Integer totalNumber = getParaToInt("totalNumber");
        Coupon coupon = Coupon.dao.findById(couponId);
        if(coupon !=null ){
            Integer surplus = totalNumber - coupon.getTotalNumber() + coupon.getSurplusNumber();
            if(surplus < 0){
                returnJson(-1,"剩余数量不足,请提高数量!");
                return;
            }
            coupon.setStartTime(startTime);
            coupon.setEndTime(endTime);
            coupon.setTotalNumber(totalNumber);
            coupon.setSurplusNumber(surplus);
            if(coupon.update()){
                returnSuccessJson();
            }else{
                returnFailureJson();
            }
        }else{
            returnJson(0,"找不到该优惠券");
        }
    }

    public void delete(){
        Integer couponId = getParaToInt("couponId");
        Coupon coupon = Coupon.dao.findById(couponId);
        if(coupon !=null){
            coupon.setDeleteTime(new Date());
            if(coupon.update()){
                returnSuccessJson();
            }else{
                returnFailureJson();
            }
        }else{
            returnJson(0,"找不到该优惠券");
        }

    }

}
